/**
 * src/pages下，所有model.ts文件
 * 默认返回一个对象
 * 导出的类型默认会挂在到umi上，比如下面的ModalState和ModalType
 */
import { Effect, Reducer } from 'umi';
import { login } from './service';

export interface ModalState {
  name: string;
}
// 定义model返回对象的类型
export interface ModalType {
  namespace: string;
  state: ModalState;
  effect: {
    login: Effect;
  };
  reducers: {
    save: Reducer<ModalState>;
  };
}
export default {
  namespace: 'login',
  state: {
    name: '',
  },
  effects: {
    // *login(参数1,参数2) {},
    *login({ payload }: any, { call, put }: any) {
      yield call(login, payload);
    },
  },
  reducers: {
    /**
     *  用于保存异步请求接口后返回的数据到state中。
     * @param state 上面定义的state
     * @param action effects中或页面中调用该函数传来的新的state
     * @returns
     */
    save(state: ModalState, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {}, //待补充
};
